﻿using System;
using System.Net;
using Microsoft.Win32;

namespace DVDFilmoteka.Settings
{
    class SettingsManager
    {
        private static readonly SettingsManager instance = new SettingsManager();

        public Boolean AutomaticUpdate { get; set; }
        private const String automaticUpdateKey = "autoUpdate";
        public Boolean UseProxy { get; set; }
        private const String useProxyKey = "useProxy";
        public Boolean UseDefaultProxy { get; set; }
        private const String useDefaultProxyKey = "useDefaultProxy";
        public IPAddress ProxyIPAddress { get; set; }
        private const String proxyIPAddressKey = "proxyIP";
        public Int32 ProxyPort { get; set; }
        private const String proxyPortKey = "proxyPort";
        public Boolean ViewResults { get; set; }
        private const String viewResultsKey = "viewResults";
        public Boolean DontChangeNames { get; set; }
        private const String dontChangeNames = "dontChangeNames";
        public Boolean DontChangeGenres { get; set; }
        private const String dontChangeGenres = "dontChangeGenres";
        public Boolean LoadPicturesOnline { get; set; }
        private const String loadPicturesOnline = "loadPicturesOnline";
        public Boolean BetaInform { get; set; }
        private const String betaInform = "betaInform";

        /// <summary>
        /// Konstruktor
        /// </summary>
        private SettingsManager()
        {
            AutomaticUpdate = true;
            UseProxy = false;
            UseDefaultProxy = true;
            ProxyIPAddress = null;
            ProxyPort = 0;
            ViewResults = true;
            DontChangeNames = false;
            DontChangeGenres = false;
            LoadPicturesOnline = true;
            BetaInform = true;

            try
            {
                loadFromRegister();
            } catch (Exception) { }
        }

        /// <summary>
        /// Tovarni metoda
        /// </summary>
        /// <returns></returns>
        public static SettingsManager Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Nacte nastaveni z registru
        /// </summary>
        public void loadFromRegister()
        {
            RegistryKey regKeyAppRoot = Registry.CurrentUser.OpenSubKey("Software\\MJ Design\\DVD Filmoteka");
            if (regKeyAppRoot == null)
            {
                regKeyAppRoot = Registry.LocalMachine.OpenSubKey("Software\\MJ Design\\DVD Filmoteka");
                if (regKeyAppRoot == null) return;
            }

            AutomaticUpdate = "true".Equals((string) regKeyAppRoot.GetValue(automaticUpdateKey, "true"));
            UseProxy = "true".Equals((string)regKeyAppRoot.GetValue(useProxyKey, "false"));
            UseDefaultProxy = "true".Equals((string)regKeyAppRoot.GetValue(useDefaultProxyKey, "true"));
            ViewResults = "true".Equals((string)regKeyAppRoot.GetValue(viewResultsKey, "true"));
            DontChangeNames = "true".Equals((string)regKeyAppRoot.GetValue(dontChangeNames, "false"));
            DontChangeGenres = "true".Equals((string)regKeyAppRoot.GetValue(dontChangeGenres, "false"));
            LoadPicturesOnline = "true".Equals((string)regKeyAppRoot.GetValue(loadPicturesOnline, "true"));
            BetaInform = "true".Equals((string)regKeyAppRoot.GetValue(betaInform, "true"));

            // Adresa proxy serveru
            String value = (string) regKeyAppRoot.GetValue(proxyIPAddressKey, "");
            IPAddress address;
            Boolean b = IPAddress.TryParse(value, out address);
            if (b) ProxyIPAddress = address;

            // Port proxy serveru
            value = (string)regKeyAppRoot.GetValue(proxyPortKey, "");
            Int32 result;
            b = Int32.TryParse(value, out result);
            if (b) ProxyPort = result;
        }

        /// <summary>
        /// Ulozi nastaveni do registru
        /// </summary>
        public void saveToRegister()
        {
            RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey("Software\\MJ Design\\DVD Filmoteka");
            if (regKeyAppRoot == null) return;

            // Automaticky update
            if (AutomaticUpdate)
            {
                regKeyAppRoot.SetValue(automaticUpdateKey, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(automaticUpdateKey, "false");
            }

            // Vysledky hledani
            if (ViewResults)
            {
                regKeyAppRoot.SetValue(viewResultsKey, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(viewResultsKey, "false");
            }

            // Pouzit proxy
            if (UseProxy)
            {
                regKeyAppRoot.SetValue(useProxyKey, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(useProxyKey, "false");
            }

            // Pouzit defaultni proxy
            if (UseDefaultProxy)
            {
                regKeyAppRoot.SetValue(useDefaultProxyKey, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(useDefaultProxyKey, "false");
            }

            // Adresa proxy serveru
            if (ProxyIPAddress == null)
            {
                regKeyAppRoot.SetValue(proxyIPAddressKey, "");
            }
            else
            {
                regKeyAppRoot.SetValue(proxyIPAddressKey, ProxyIPAddress.ToString());
            }

            // Nemenit nazvy filmu
            if (DontChangeNames)
            {
                regKeyAppRoot.SetValue(dontChangeNames, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(dontChangeNames, "false");
            }

            // Nemenit zanry filmu
            if (DontChangeGenres)
            {
                regKeyAppRoot.SetValue(dontChangeGenres, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(dontChangeGenres, "false");
            }

            // Nacitat obrazky online
            if (LoadPicturesOnline)
            {
                regKeyAppRoot.SetValue(loadPicturesOnline, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(loadPicturesOnline, "false");
            }

            // Upozornovat na betaverze
            if (BetaInform)
            {
                regKeyAppRoot.SetValue(betaInform, "true");
            }
            else
            {
                regKeyAppRoot.SetValue(betaInform, "false");
            }

            // Port proxy serveru
            regKeyAppRoot.SetValue(proxyPortKey, ProxyPort.ToString());
        }
    }
}